"""运行环境的基础配置
"""
import argparse
import os.path
import sys
import constant
from utils.utils_log import loggerCase
from utils.utils_file import *
from utils.read_config import readconfig

current_time = time.strftime("%Y%m%d%H%M%S")



def command_line_paramaeter():
    """
    接收命令行参数
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('app_dir', help='app路径')
    parser.add_argument('--smoke_mode', help='是否执行性能模块')
    parser.add_argument('--case_type', help='测试用例等级')
    parser.add_argument('--case_num', help='测试用例执行次数')

    args = parser.parse_args()
    smoke_mode = args.smoke_mode
    case_type = args.case_type
    case_num = args.case_num
    print(f'-------{args.app_dir}----------{smoke_mode}---------{case_type}--{case_num}----------')
    return smoke_mode, case_type, case_num


def setup(*args):
    """复制指定的配置文件及存图到指定目录
    """
    loggerCase.debug("开始复制ConfigEditor.exe")
    origin_config_editor = constant.file_path + '/ConfigEditor.exe'
    new_config_editor = readconfig.soft_install_path + '/ConfigEditor.exe'
    copy_file(origin_config_editor, new_config_editor)

    loggerCase.debug("开始复制key.sn")
    origin_key_sn = constant.file_path + '/key.sn'
    new_key_sn = readconfig.soft_install_path + '/res/key.sn'
    copy_file(origin_key_sn, new_key_sn, is_cover=True)

    loggerCase.debug("开始复制config.db")
    origin_config_db = constant.file_path + '/config.db'
    new_config_db = readconfig.soft_install_path + '/res/config.db'
    copy_file(origin_config_db, new_config_db, is_cover=True)

    loggerCase.debug("开始复制读图文件")
    FreeTrak = readconfig.soft_install_path + '/res/FreeScanOmni/'
    cope_folder(source_path=constant.read_picture_path, target_path=FreeTrak, is_cover=True)

    loggerCase.debug("开始langconfig文件")
    origin_langconfig_ini = constant.file_path + '/langconfig.ini'
    new_langconfig_ini = r'C:\Users\Public\shining3d\FreeScan Omni' + '/langconfig.ini'
    copy_file(origin_langconfig_ini, new_langconfig_ini, is_cover=True)

def pre_setting():
    """卸载软件包并安装新的软件包
    """
    install_path = readconfig.soft_install_path
    arguments = sys.argv
    soft_package = arguments[1] + '.exe'
    print(install_path)
    if os.path.exists(rf'{install_path}\FreeScan Omni.exe'):
        # 除了需要保存的文件，其余都删除
        loggerCase.info("清理环境开始")
        path = os.path.normpath(fr'"{install_path}\uninst.exe"')

        os.system(f"{path} /S")
        time.sleep(10)
    while os.path.exists(rf"{install_path}\FreeScan Omni.exe"):
        loggerCase.info("---等待清理环境中---")
        time.sleep(5)
    loggerCase.info("清理环境结束")
    if not os.path.exists(rf"{install_path}\FreeScan Omni.exe"):
        loggerCase.info(f"安装新的包：{soft_package}")
        os.system(rf"{soft_package} /S")

    while not os.path.exists(rf"{install_path}\FreeScan Omni.exe"):
        loggerCase.info("新包安装中")
        time.sleep(3)
        loggerCase.info("新包安装成功")


if __name__ == '__main__':
    pre_setting()
    setup()